package controller;

import impl.service.MusicServer;
import impl.service.MusicSql;
import model.MusicData;
import model.MusicList;
import model.ResponseBodyMessage;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

public class MusicController {
    private static final MusicSql MUSIC_SQL = new MusicSql();
    public ResponseBodyMessage<Boolean> insertToLove(String url,Integer id){
        System.out.println("url:" + url);
        System.out.println("id:" + id);
        try {
            return new ResponseBodyMessage<>(true,"查询成功",MUSIC_SQL.insertToLove(url,id));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public ResponseBodyMessage<Boolean> insertToList(String url,Integer id){
        try {
            return new ResponseBodyMessage<>(true,"查询成功",MUSIC_SQL.insertToList(url,id));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    //获取歌单音乐
    public ResponseBodyMessage<List<MusicData>> getListMusic(Integer id){
        try {
            return new ResponseBodyMessage<>(true,"查询成功",MUSIC_SQL.getMusicList(id));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    //创建歌单
    public ResponseBodyMessage<Boolean> createList(Integer userId,String name){
        try {

            return new ResponseBodyMessage<>(true,"创建成功",MUSIC_SQL.createMusicList(name,userId));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    //删除歌单
    public ResponseBodyMessage<Boolean> deleteList(Integer id){
        try {
            return new ResponseBodyMessage<>(true,"删除成功",MUSIC_SQL.deleteList(id));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 查询全部歌曲
     * @return
     */
    public ResponseBodyMessage<List<MusicData>> getMusic(){
        try {
            return new ResponseBodyMessage<List<MusicData>>(true, "查询成功", MUSIC_SQL.getAllMusic());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 查询喜欢的歌曲
     * @return
     */
    public ResponseBodyMessage<List<MusicData>> getLoveMusic(Integer userId){
        try {
            return new ResponseBodyMessage<List<MusicData>>(true,"查询成功",MUSIC_SQL.getLoveMusic(userId));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * 查询最近播放音乐
     */
    public ResponseBodyMessage<List<MusicData>> getLatelyMusic(Integer userId){
        try {
            return new ResponseBodyMessage<List<MusicData>>(true,"查询成功",MUSIC_SQL.getlatelyMusic(userId));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    //获取音乐歌单
    public ResponseBodyMessage<List<MusicList>> getlist(Integer userId){
        try {
            System.out.println("id:" + userId);
            return new ResponseBodyMessage<>(true,"查询成功",MUSIC_SQL.getMuiscListName(userId));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public ResponseBodyMessage<Boolean> update(Integer userId,String url){
        try {
            Integer id = MUSIC_SQL.getMusicId(url);
            MUSIC_SQL.updateCount(url);
            MUSIC_SQL.updateLately(userId,id);
            return new ResponseBodyMessage<>(true,"更新成功",true);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public ResponseBodyMessage<List<MusicData>> getSearchMusic(String search) {
        try {
            return new ResponseBodyMessage<>(true,"查询成功",MUSIC_SQL.search(search));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public ResponseBodyMessage<Boolean> deleteMusic(Integer id, String musicId) {
        try {
            return new ResponseBodyMessage<>(true,"删除成功",MUSIC_SQL.delectMusicForList(id,musicId));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public ResponseBodyMessage<Boolean> deleteMusicForLove(Integer id, String musicId) {
        try {
            return new ResponseBodyMessage<>(true,"删除成功",MUSIC_SQL.delectMusicForLove(id,musicId));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
